<template>
    <div>
        <a-modal
            :width="1000"
            title="选择模板"
            :visible="selectTemplateVisible"
            @ok="selectTemplateHandleOk"
            @cancel="selectTemplateVisible = false"
            :forceRender="true"
        >
            <commpVxeTable
                :key="selectTemplateKey"
                :config="selectTemplateConfig" 
                :columns="selectTemplatecolumnsVxe" 
                :formItem="selectTemplateformItem"
                @radioChange="selectTemplateChange"
                ref="acceptanceRegistrationRef"
                :defaultSelected="{key:'xzid'}"
                @getDefaultSelected="getDefaultSelected"
            >
            </commpVxeTable>
    </a-modal>
    </div>
</template>
<script>
import commpVxeTable from '@/components/commp-vxe-table/commp-vxe-table.vue'
import assessCheckApi from './service/api'
import {  selectWjmbServe,commoninitWjjl, spcydownloadSqsxFileMb } from './service/index'
import blobType from '@/common/blobType'
export default {
    components:{
        commpVxeTable,
    },
    props:{

    },
    data(){
        return{
            selectTemplateVisible:false,
            selectTemplateKey:null,
            templateId:'',
            takeNotesId:'',
            takeNotesType:'',
            selectTemplateConfig:{
                api:'',
                rowId: 'id',
                fywflcode:blobType.fywflcodeArr[blobType.menuType.indexOf(this.$route.name.substring(this.$route.name.indexOf('__'),this.$route.name.indexOf('_a')+2))],
                height:400,
                autoHidden:true,
                pageSize:100,
                selectType:'radio',
                listData:'wjmbList',
                paramet:{
                }
            },
            selectTemplatecolumnsVxe:[{
                type:'radio',
                width:60,
            },{
                title: '模板标题', //列头显示文字
                field: 'fmbbt',
                key: 'fmbbt',
            },{
                title: '文件类型', //列头显示文字
                field: 'fwjlx',
                key: 'fwjlx',
            },{
                title: '模板类型1', //列头显示文字
                field: 'fmblx1',
                key: 'fmblx1',
            }],
            sqsxid:'',
            selectTemplateformItem:[{
                label: '模板标题',
                model: 'fmbbt',
            }],
            selectFileSaveType:'',
            templateRow:{},
        }
    },
    watch:{

    },
    mounted(){
        
    },
    methods:{
        getSelectTemplate(row,val,sqsxid,fhbsxzt){
            this.selectFileSaveType = row.selectFileSaveType
            this.sqsxid = sqsxid
            this.templateId = ''
            // this.selectTemplateVisible = true
            this.selectTemplateConfig = {
                api:fhbsxzt == 1?assessCheckApi.spcyRsWjmbqueryHbjcMbList:assessCheckApi.queryYwflWjmbList,
                rowId: 'id',
                fywflcode:blobType.fywflcodeArr[blobType.menuType.indexOf(this.$route.name.substring(this.$route.name.indexOf('__'),this.$route.name.indexOf('_a')+2))],
                height:400,
                autoHidden:true,
                pageSize:100,
                selectType:'radio',
                listData:'wjmbList',
                paramet:{
                    sqsxid:sqsxid,
                    type:val
                }
            },
            this.selectTemplateKey = Date.now()
            this.takeNotesId = row.id
            this.takeNotesType = val
        },
        getDefaultSelected(val){
            this.templateId = val
        },
        selectTemplateChange(val){
            this.templateId = val.row.id
            this.templateRow = val.row
        },
        selectTemplateHandleOk(){
            if(this.templateId){
                var that = this
                var fileName = this.templateRow.fmbbt
                let param = { id:this.templateId,sqsxid:this.sqsxid}
                spcydownloadSqsxFileMb(param).then((data) => {
                    if (!data) {
                        that.$message.warning('文件下载失败')
                    return
                    }
                    let url = window.URL.createObjectURL(new Blob([data], { type: blobType.type[blobType.suffix.indexOf(this.templateRow.fwjlx)] }))
                    let link = document.createElement('a')
                    link.style.display = 'none'
                    link.href = url
                    link.setAttribute('download', fileName + this.templateRow.fwjlx)
                    document.body.appendChild(link)
                    link.click()
                    document.body.removeChild(link) //下载完成移除元素
                    window.URL.revokeObjectURL(url) //释放掉blob对象
                    that.$message.success('已加入下载列表')
                    this.selectTemplateVisible = false
                })
            }else{
                this.$message.warning('请选择模板')
            }
            
        },
    }
}
</script>